package com.hanlink.dsp.ui.widget;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.ActionMode;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.qztech.sdk.R;
import com.qztech.utils.YzDebug;

import java.util.Arrays;


/**
 * Created by Administrator on 2018/9/5.
 */

public class EqEditText extends androidx.appcompat.widget.AppCompatEditText implements TextView.OnEditorActionListener {
    private final int WHAT_SHOW_SOFTINPUT = 0x100;
    private Handler mHandler;
    private ViewTreeObserver.OnGlobalLayoutListener mOnGlobalLayoutListener;
    public EqEditText(Context context) {
        super(context);
        init(context, null);
    }

    public EqEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public EqEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);

    }
    private boolean isHasFocus;
    private void init(Context context, AttributeSet attrs){

        setLongClickable(false);
        setOnEditorActionListener(this);
        mHandler = new android.os.Handler(context.getMainLooper()){
            @Override
            public void handleMessage(@NonNull Message msg) {
                EqEditText.this.handleMessage(msg);
            }
        };
        mOnGlobalLayoutListener = () -> {
            if (hasFocus()){
                if (hasFocus()){
                    if (isHasFocus){
                        isHasFocus = false;
                        clearFocus();
                    }else {
                        isHasFocus = true;
                    }
                }
            }
        };

        /*setCustomSelectionActionModeCallback(new ActionMode.Callback() {
            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                YzDebug.d("onCreateActionMode:");
                mode.finish();
                return true;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                YzDebug.d("onPrepareActionMode");
                return false;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                YzDebug.d("onActionItemClicked");
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {
                YzDebug.d("onDestroyActionMode");
            }
        });*/
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        isHasFocus = false;
        getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        getViewTreeObserver().removeOnGlobalLayoutListener(mOnGlobalLayoutListener);
    }

    private void handleMessage(@NonNull Message msg) {
        if (msg == null)return;
        switch (msg.what){
            case WHAT_SHOW_SOFTINPUT:
                setSelection(getEditableText().length());
                requestFocus();
                InputMethodManager imm = getInputMethodManager();
                if (imm != null  ) {
                    imm.showSoftInput(this, 0);
                }
                break;
        }
    }
    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        YzDebug.d("onKeyPreIme:"+ keyCode);
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
            validEditTextValue();
        }
        return super.onKeyPreIme(keyCode, event);
    }

    public void validEditTextValue() {
        if (mValidEditTextListener != null) {
            mValidEditTextListener.onValidEditTextValue();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (hasFocus()){
            return super.onTouchEvent(event);
        }
//        YzDebug.d("onTouchEvent action:"+ event.getAction());
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                mHandler.sendEmptyMessageDelayed(WHAT_SHOW_SOFTINPUT, 1);
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                mHandler.removeMessages(WHAT_SHOW_SOFTINPUT);
                break;
        }
        return true;
    }
    private InputMethodManager mInputMethodManager;
    private InputMethodManager getInputMethodManager() {
        if (mInputMethodManager == null){
            mInputMethodManager = getContext().getSystemService(InputMethodManager.class);
        }
        return mInputMethodManager;
    }
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//        YzDebug.d("onEditorAction id:" +actionId);
        validEditTextValue();
        return false;
    }

    public interface ValidEditTextListener {
        void onValidEditTextValue();
    }

    private ValidEditTextListener mValidEditTextListener;

    public void setValidEditTextListener(ValidEditTextListener validEditTextListener) {
        this.mValidEditTextListener = validEditTextListener;
    }

}
